package com.wushijia.test;

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;


public class Demo01 {

//  private static Logger logger = new Logger();

  private static ActionEvent actionEvent;

  private static void test() {
    Button button = new Button();
    button.addActionListener(event -> actionEvent = event);
//    logger.debugger(() -> "debugger");
  }

  public static long measureSumPerf(Function<Long, Long> function, long n) {
    long fastest = Long.MAX_VALUE;
    for (int i = 0; i < 10; i++) {
      long start = System.nanoTime();
      long sum = function.apply(n);
      long duration = (System.nanoTime() - start) / 1000;
      System.out.println("Result:" + sum);
      if (duration < fastest) {
        fastest = duration;
      }
    }
    return fastest;
  }

  public static void main(String[] args) {
    System.out.println("hello world");
    BinaryOperator<Long> add = (x, y) -> x + y;
    //Lambda表达式的类型依赖于上下文环境，是由编译器推断出来的
    //目标类型是指lambda表达式所在上下文环境的类型，比如，将lambda表达n式赋值给一个局部变量或传递给一个方法作为参数，局部变量或方法参数的类型就是lambda表达式的目标类型
    BinaryOperator<Long> addExplicit = (Long x, Long y) -> x + y;
    //类型推断，也需要根据一定的信息推断，比如code-I中需要给定具体泛型的类型为Integer，否则认为参数和返回值的类型都为Object
    Predicate<Integer> predicate = x -> x > 5;//code-I
    BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> x > y ? 1 : 2;
    Objects.requireNonNull(add);

    test();

    //无法通过lambda表达式赋值局部变量
    //ActionEvent actionEvent2;
    //Button button = new Button();
    //button.addActionListener(event -> actionEvent2 = event);

    System.out.println(ParallelStreams.sequentialSum(10L));
    //并行流使用的线程数量默认等于处理器数量
    System.out.println("处理器数量：" + Runtime.getRuntime().availableProcessors());

    System.out.println(measureSumPerf(ParallelStreams::sequentialSum, 1000L));
  }
}
